<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>openGauss Blog  | openGauss1.0.1支持docker主备搭建</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">

    
    <link rel="shortcut icon" href="https://xzx666.gitee.io/lookeng/img/favicon.ico" type="image/x-icon" />
    <link rel="apple-touch-icon" href="https://xzx666.gitee.io/lookeng/img/apple-touch-icon.png" />

    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

    
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
    
    <link rel="stylesheet" href="https://xzx666.gitee.io/lookeng/css/commen.css?t=1607593672000" />
    <link rel="stylesheet" href="https://xzx666.gitee.io/lookeng/css/blog.css?t=1607593672000" />
    <link rel="stylesheet" href="https://xzx666.gitee.io/lookeng/css/mobile.css?t=1607593672000" />
    <link rel="stylesheet" href="https://xzx666.gitee.io/lookeng/css/pagination.css?t=1607593672000">
    
    <script>
      var _hmt = _hmt || [];
      (function() {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?34e9f585f29581007941aa1698181871";
        var s = document.getElementsByTagName("script")[0]; 
        s.parentNode.insertBefore(hm, s);
      })();
    </script>
</head>
<body>
  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src='//unpkg.com/valine/dist/Valine.min.js'></script>






<div class="container">
    <div class="columns blog-detail">
        <div class="post_detail">
            <div class="is-child box">
                <div class="breadCrumb"><a href="\zh\">博客/</a></div>
                <div class="blog-detail-header">
                    <h1>openGauss1.0.1支持docker主备搭建</h1>
                    <div class="blog-detail-prop">
                        <img src="https://xzx666.gitee.io/lookeng/img/icon-date.png">
                        <span class="article_right_date">2020-11-19</span>
                        <img src="https://xzx666.gitee.io/lookeng/img/icon-author.png">
                        <span class="article_right_author">多米爸比</span>
                        <img src="https://xzx666.gitee.io/lookeng/img/icon-view.png">
                        <span id="/zh/post/duomibabi/opengauss1-0-1%E6%94%AF%E6%8C%81docker%E4%B8%BB%E5%A4%87%E6%90%AD%E5%BB%BA/" class="leancloud_visitors" data-flag-title="openGauss1.0.1支持docker主备搭建">
                            <i class="leancloud-visitors-count"></i>                     
                        </span>
                    </div>
                    <div class="blog-detail-tags">
                        <img src="https://xzx666.gitee.io/lookeng/img/icon-tag.png">
                        
                        <a href="https://xzx666.gitee.io/lookeng/zh/tags/opengauss1.0.1%E6%94%AF%E6%8C%81docker%E4%B8%BB%E5%A4%87%E6%90%AD%E5%BB%BA">openGauss1.0.1支持docker主备搭建</a>
                        
                    </div>
                </div>
                <div class="content blog-content">
                    

<h1 id="opengauss1-0-1支持docker主备搭建-a-name-zh-cn-topic-0291959531-a">openGauss1.0.1支持docker主备搭建<a name="ZH-CN_TOPIC_0291959531"></a></h1>

<p>目前已经支持x86-64和ARM64两种架构。</p>

<p>x86-64架构的openGuass运行在CentOS 7.6操作系统中。</p>

<p>ARM64架构的openGauss运行在openEuler 20.03 LTS操作系统中。</p>

<h2 id="快速搭建环境-a-name-zh-cn-topic-0292876855-a">快速搭建环境<a name="ZH-CN_TOPIC_0292876855"></a></h2>

<h3 id="pull镜像文件-a-name-section241153503312-a">pull镜像文件<a name="section241153503312"></a></h3>

<pre><code>docker pull enmotech/opengauss:1.0.1
</code></pre>

<h3 id="创建自定义网络-创建容器便于使用固定ip-a-name-section827385053313-a">创建自定义网络，创建容器便于使用固定IP<a name="section827385053313"></a></h3>

<pre><code>docker network create --subnet=172.18.0.0/16 myNetwork
</code></pre>

<h3 id="创建主库容器并运行-a-name-section18401141113349-a">创建主库容器并运行<a name="section18401141113349"></a></h3>

<pre><code>docker run --name op_master \
--network myNetwork --ip 172.18.0.10 --privileged=true \
--hostname op_master --detach \
--env GS_PORT=6432 \
--env OG_SUBNET=172.18.0.0/16 \
--env GS_PASSWORD=Enmotech@2020 \
--env NODE_NAME=op_master \
--env REPL_CONN_INFO=&quot;replconninfo1 = 'localhost=172.18.0.10 localport=6439 localservice=6432 remotehost=172.18.0.11 remoteport=6439 remoteservice=6432 '\n&quot; \
--cpuset-cpus=&quot;1,3&quot; \
opengauss:1.0.1 -M primary
</code></pre>

<h3 id="创建备库容器并运行-a-name-section17112739193013-a">创建备库容器并运行<a name="section17112739193013"></a></h3>

<pre><code>docker run --name op_slave_one \
--network myNetwork --ip 172.18.0.11 --privileged=true \
--hostname op_slave_one --detach \
--env GS_PORT=6432 \
--env OG_SUBNET=172.18.0.0/16 \
--env GS_PASSWORD=Enmotech@2020 \
--env NODE_NAME=op_slave_one \
--env REPL_CONN_INFO=&quot;replconninfo1 = 'localhost=172.18.0.11 localport=6439 localservice=6432 remotehost=172.18.0.10 remoteport=6439 remoteservice=6432 '\n&quot; \
--cpuset-cpus=&quot;2,4&quot; \
opengauss:1.0.1 -M standby
</code></pre>

<p>说明：可以参考<a href="https://hub.docker.com/r/enmotech/opengauss">官方镜像脚本</a>来创建localport与localservice端口间隔要大于2，对外映射端口为localservice。</p>

<h2 id="查询主备角色状态-a-name-zh-cn-topic-0292876856-a">查询主备角色状态<a name="ZH-CN_TOPIC_0292876856"></a></h2>

<h3 id="查询命令-a-name-section89192717352-a">查询命令<a name="section89192717352"></a></h3>

<pre><code>gs_ctl query -D /var/lib/opengauss/data/
</code></pre>

<h3 id="主库op-master查询-a-name-section1237916165365-a">主库op_master查询<a name="section1237916165365"></a></h3>

<pre><code>docker exec -it op_master bash

[omm@op_master ~]$ gs_ctl query -D /var/lib/opengauss/data/
[2020-10-20 13:50:39.163][666][][gs_ctl]: gs_ctl query ,datadir is -D &quot;/var/lib/opengauss/data&quot;  
 HA state:           
	local_role                     : Primary
	static_connections             : 1
	db_state                       : Normal
	detail_information             : Normal

 Senders info:       
	sender_pid                     : 637
	local_role                     : Primary
	peer_role                      : Standby
	peer_state                     : Normal
	state                          : Streaming
	sender_sent_location           : 0/5000238
	sender_write_location          : 0/5000238
	sender_flush_location          : 0/5000238
	sender_replay_location         : 0/5000238
	receiver_received_location     : 0/5000238
	receiver_write_location        : 0/5000238
	receiver_flush_location        : 0/5000238
	receiver_replay_location       : 0/5000238
	sync_percent                   : 100%
	sync_state                     : Sync
	sync_priority                  : 1
	sync_most_available            : On
	channel                        : 172.18.0.10:6439--&gt;172.18.0.11:55440

 Receiver info:      
No information 
</code></pre>

<h3 id="备库op-slave-one查询-a-name-section15682842183712-a">备库op_slave_one查询<a name="section15682842183712"></a></h3>

<pre><code>docker exec -it op_slave_one bash

[omm@op_slave_one ~]$ gs_ctl query -D /var/lib/opengauss/data/
[2020-10-20 13:51:24.640][460][][gs_ctl]: gs_ctl query ,datadir is -D &quot;/var/lib/opengauss/data&quot;  
 HA state:           
	local_role                     : Standby
	static_connections             : 1
	db_state                       : Normal
	detail_information             : Normal

 Senders info:       
No information 
 Receiver info:      
	receiver_pid                   : 401
	local_role                     : Standby
	peer_role                      : Primary
	peer_state                     : Normal
	state                          : Normal
	sender_sent_location           : 0/5000238
	sender_write_location          : 0/5000238
	sender_flush_location          : 0/5000238
	sender_replay_location         : 0/5000238
	receiver_received_location     : 0/5000238
	receiver_write_location        : 0/5000238
	receiver_flush_location        : 0/5000238
	receiver_replay_location       : 0/5000238
	sync_percent                   : 100%
	channel                        : 172.18.0.11:55440&lt;--172.18.0.10:6439
</code></pre>

<p>说明：从上面主库Senders信息和备库Receiver可以看到主备状态正常。</p>

<h2 id="读写及切换测试-a-name-zh-cn-topic-0292876857-a">读写及切换测试<a name="ZH-CN_TOPIC_0292876857"></a></h2>

<h3 id="主库op-master写测试-a-name-section7258313549-a">主库op_master写测试<a name="section7258313549"></a></h3>

<pre><code>[omm@op_master ~]$ gsql -p6432
gsql ((openGauss 1.0.1 build e9da9fb9) compiled at 2020-10-01 20:21:42 commit 0 last mr  )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type &quot;help&quot; for help.

omm=# create table t(id json);
CREATE TABLE
omm=# insert into t values('{&quot;name&quot;:&quot;Mr.D&quot;}');
INSERT 0 1
</code></pre>

<h3 id="备库op-slave-one读测试-a-name-section12810194412546-a">备库op_slave_one读测试<a name="section12810194412546"></a></h3>

<pre><code>[omm@op_slave_one ~]$ gsql -p6432
gsql ((openGauss 1.0.1 build e9da9fb9) compiled at 2020-10-01 20:21:42 commit 0 last mr  )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type &quot;help&quot; for help.

omm=# select * from t;
       id        
-----------------
 {&quot;name&quot;:&quot;Mr.D&quot;}
(1 row)

omm=# delete from t;
ERROR:  cannot execute DELETE in a read-only transaction
</code></pre>

<h3 id="切换测试-将op-slave-one-切换为主库-op-master-切换为备库-a-name-section20524191625210-a">切换测试：将op_slave_one 切换为主库，op_master 切换为备库<a name="section20524191625210"></a></h3>

<p><strong>op_slave_one执行switchover</strong></p>

<pre><code>[omm@op_slave_one ~]$ gs_ctl switchover -D /var/lib/opengauss/data/
[2020-10-20 13:57:02.877][504][][gs_ctl]: gs_ctl switchover ,datadir is -D &quot;/var/lib/opengauss/data&quot;  
[2020-10-20 13:57:02.877][504][][gs_ctl]: switchover term (1)
[2020-10-20 13:57:02.888][504][][gs_ctl]: waiting for server to switchover.........
[2020-10-20 13:57:08.920][504][][gs_ctl]: done
[2020-10-20 13:57:08.920][504][][gs_ctl]: switchover completed (/var/lib/opengauss/data)
</code></pre>

<p><strong>op_slave_one查询状态</strong></p>

<pre><code>[omm@op_slave_one ~]$ gs_ctl query -D /var/lib/opengauss/data/
[2020-10-20 13:58:13.340][555][][gs_ctl]: gs_ctl query ,datadir is -D &quot;/var/lib/opengauss/data&quot;  
 HA state:           
	local_role                     : Primary
	static_connections             : 1
	db_state                       : Normal
	detail_information             : Normal

 Senders info:       
	sender_pid                     : 523
	local_role                     : Primary
	peer_role                      : Standby
	peer_state                     : Normal
	state                          : Streaming
	sender_sent_location           : 0/5004A10
	sender_write_location          : 0/5004A10
	sender_flush_location          : 0/5004A10
	sender_replay_location         : 0/5004A10
	receiver_received_location     : 0/5004A10
	receiver_write_location        : 0/5004A10
	receiver_flush_location        : 0/5004A10
	receiver_replay_location       : 0/5004A10
	sync_percent                   : 100%
	sync_state                     : Sync
	sync_priority                  : 1
	sync_most_available            : On
	channel                        : 172.18.0.11:6439--&gt;172.18.0.10:39314

 Receiver info:      
No information   
</code></pre>

<p><strong>op_master查询状态</strong></p>

<pre><code>[omm@op_master ~]$ gs_ctl query -D /var/lib/opengauss/data/
[2020-10-20 13:58:42.827][743][][gs_ctl]: gs_ctl query ,datadir is -D &quot;/var/lib/opengauss/data&quot;  
 HA state:           
	local_role                     : Standby
	static_connections             : 1
	db_state                       : Normal
	detail_information             : Normal

 Senders info:       
No information 
 Receiver info:      
	receiver_pid                   : 739
	local_role                     : Standby
	peer_role                      : Primary
	peer_state                     : Normal
	state                          : Normal
	sender_sent_location           : 0/5004A10
	sender_write_location          : 0/5004A10
	sender_flush_location          : 0/5004A10
	sender_replay_location         : 0/5004A10
	receiver_received_location     : 0/5004A10
	receiver_write_location        : 0/5004A10
	receiver_flush_location        : 0/5004A10
	receiver_replay_location       : 0/5004A10
	sync_percent                   : 100%
	channel                        : 172.18.0.10:39314&lt;--172.18.0.11:6439
</code></pre>

<p>可以看到 op_master变为备库，op_slave_one变为主库，切换成功。</p>

<p><strong>数据读写验证</strong></p>

<p>主库op_slave_one做写入验证。</p>

<pre><code>[omm@op_slave_one ~]$ gsql -p6432
gsql ((openGauss 1.0.1 build e9da9fb9) compiled at 2020-10-01 20:21:42 commit 0 last mr  )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type &quot;help&quot; for help.

omm=# select * from t;
       id        
-----------------
 {&quot;name&quot;:&quot;Mr.D&quot;}
(1 row)

omm=# insert into t values('{&quot;name&quot;:&quot;insert from op_slave_one &quot;}');
INSERT 0 1
</code></pre>

<p>备库op_master做读取验证。</p>

<pre><code>[omm@op_master ~]$ gsql -p6432
gsql ((openGauss 1.0.1 build e9da9fb9) compiled at 2020-10-01 20:21:42 commit 0 last mr  )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type &quot;help&quot; for help.

omm=# select * from t;
                  id                  
--------------------------------------
 {&quot;name&quot;:&quot;Mr.D&quot;}
 {&quot;name&quot;:&quot;insert from op_slave_one &quot;}
(2 rows)

omm=# delete from t;
ERROR:  cannot execute DELETE in a read-only transaction
</code></pre>

                </div>
            </div>
            <div class="box" style="margin-top:15px;">
                <div style="font-size:14px;color:gray"><strong>【免责声明】</strong>本文仅代表作者本人观点，与本网站无关。本网站对文中陈述、观点判断保持中立，不对所包含内容的准确性、可靠性或完整性提供任何明示或暗示的保证。本文仅供读者参考，由此产生的所有法律责任均由读者本人承担。</div>
            </div>
            <div class="post-comment">
                
                  
  <link rel="stylesheet" href="https://xzx666.gitee.io/lookeng/css/custom.css" />
  <div id="vcomments" lang="zh-cn" mess="说点什么吧..."></div>

  <script type="text/javascript">
    const placeholder = document.getElementById("vcomments").getAttribute("mess"),
          lang = document.getElementById("vcomments").getAttribute("lang");
    const  langs = lang == "zh-cn" ? "zh-cn" : "en";
    new Valine({
        el: '#vcomments' ,
        appId: '6wfgavgIRqmpC3hjHqQVtFWF-gzGzoHsz',
        appKey: 'QRqrBDBB0p0YhrGe9IJ169ip',
        avatar:'mm', 
        placeholder: placeholder,
        visitor: true,
		    meta: ['nick','mail','link'],
        lang: langs
    });
  </script>
            </div>
        </div>
    </div>
</div>
<script>
    $(function (){
        var query = new AV.Query('Counter');
        query.equalTo('url', decodeURIComponent('\/zh\/post\/duomibabi\/opengauss1-0-1%E6%94%AF%E6%8C%81docker%E4%B8%BB%E5%A4%87%E6%90%AD%E5%BB%BA\/'));
        query.find().then(function (data) {
            if(data.length && (data[0].attributes.author === 'openGauss')){
                var counter = AV.Object.createWithoutData('Counter', data[0].id);
                counter.set('author', '多米爸比');
                counter.save();
            }
        })
    })
</script>







<input id="iframeUrl" type="text" style="display: none;" value=https://xzx666.gitee.io/>




<script defer src="https://xzx666.gitee.io/lookengjs/all.js"></script>
<script src="https://xzx666.gitee.io/lookengjs/flexible.js"></script>
<script>
  const hash = window.location.search,
        pageurl = window.location.href;
  var langss = document.querySelector("html").lang === "zh-cn" ? "zh" : "en";
  console.log("hash:",hash,"pageurl:",pageurl,"ttr",pageurl.split(langss + "/")[1])

  if(!document.getElementById("vcomments")) {
    new Valine({
        appId: '6wfgavgIRqmpC3hjHqQVtFWF-gzGzoHsz',
        appKey: 'QRqrBDBB0p0YhrGe9IJ169ip',
        avatar:'mm',
        visitor: true,
		    meta: ['nick','mail','link']
    });
  }
 
  
    


    const url = document.getElementById("iframeUrl").value;

    function observe (el, options, callback) {
      var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver
      var observer = new MutationObserver(callback)
      observer.observe(el, options)
    }

    var options = {
        childList: true,
        subtree: true,
        characterData: true
      }
    observe(document.body, options, (records, instance) => {
    const height = document.body.scrollHeight;
    parent.postMessage(height,url);
    })
    if(pageurl.split(langss + "/")[1]) parent.postMessage(pageurl.split(langss + "/")[1],url);
    

    if(document.querySelector("#notFound")) parent.postMessage("我404了",url);

</script>
</body>
</html>
